from flask import Blueprint, request

from py.Services.MessageService import MessageService
from py.Services.SummaryService import SummaryService
from py.commonResponse.CommonResponse import *
from py.commonResponse.Dict import Dict

bp_msg = Blueprint('message', __name__, url_prefix="/messages")  # 蓝图使用方法，参数里给定文件名，还可以给定url前缀


#  增加消息
@bp_msg.route('', methods=['POST'])
def message_Insert():
    message = request.json
    msg = MessageService.Message_Insert(Dict(message))
    return createForSuccessWM(msg)


# 获取所有消息
@bp_msg.route('', methods=['GET'])
def message_Query_All():
    messageList = MessageService.Message_Query_All()
    messageList1 = []
    for message in messageList:
        messageList1.append(dict(message))
    if messageList1 is None:
        return createForErrorWM('没有读者留言哦')
    return createForSuccessWMD('查找成功', messageList1)


# 通过id查找他的message
@bp_msg.route('<int:id>', methods=['GET'])
def message_Query_ByID(id):
    messageList = MessageService.Message_Query_ByID(id)
    messageList1 = []
    for message in messageList:
        messageList1.append(dict(message))
    if not messageList1:
        return createForErrorWM('没找到这个id的留言哦')
    return createForSuccessWMD('查找成功', messageList1)


# 根据时间间隔删除信息
@bp_msg.route('/duration/<int:Duration>', methods=['DELETE'])
def message_Delete_ByDuration(Duration):
    msg = MessageService.Message_Delete_ByDuration(Duration)
    return createForSuccessWM(msg)


# 根据id删除信息
@bp_msg.route('/id/<int:id>', methods=['DELETE'])
def message_Delete_ByID(id):
    msg = MessageService.Message_Delete_ByID(id)
    return createForSuccessWM(msg)


# 根据某天删除之前的信息
@bp_msg.route('/datetime/<string:date_str>', methods=['DELETE'])
def message_Delete_ByDateTime(date_str):
    date = datetime.strptime(date_str, '%Y-%m-%d')
    msg = MessageService.Message_Delete_ByDateTime(date)
    return createForSuccessWM(msg)
